/*
 * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <climits>
#include <sys/types.h>
#include <gtest/gtest.h>
#include <dirent.h>
#include <unistd.h>
#include "It_process_plimits.h"
using namespace std;
#define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
#define CHILD_FUNC_ARG (0x2088)
#define PAGESIZE 4096
#define TESTSIZE 2097152

char *testStrArray2[10];

static int limitWrite(const char *filepath, char *buf)
{
    int fd = open(filepath, O_WRONLY);
    size_t twd = write(fd, buf, (strlen(buf) + 1));
    close(fd);
    return twd;
}

static int Clone(void *arg)
{   std::string path = "/proc/plimits/test";
    std::string limitTestPath = "/proc/plimits/test/memory.limit";
    std::string procsTestPath = "/proc/plimits/test/plimits.procs";
    std::string procsTestPath1 = "/proc/plimits/test1/plimits.procs";
    std::string failcntTestPath = "/proc/plimits/test/memory.failcnt";
    std::string usageTestPath = "/proc/plimits/test/memory.usage";
    std::string peakTestPath = "/proc/plimits/test/memory.peak";
    char writeBuf[8];

    int *strCount = (int *)arg;
    testStrArray2[*strCount] = (char *)malloc(TESTSIZE);
    if (testStrArray2[*strCount] == NULL) {
        return 1;
    }
    memset_s(testStrArray2[*strCount], TESTSIZE, 1, TESTSIZE);
    sleep(2);
    return 0;
}

static UINT32 Testcase(void)
{
    mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
    int ret, status;
    int fd;
    int num = 10;
    int twoM = 100 * 1024 * 1024;
    std::string path = "/proc/plimits/test";
    std::string path1 = "/proc/plimits/test1";
    std::string pidMaxPath = "/proc/plimits/test/pids.max";
    std::string limitTestPath = "/proc/plimits/test/memory.limit";
    std::string procsTestPath = "/proc/plimits/test/plimits.procs";
    std::string procsTestPath1 = "/proc/plimits/test1/plimits.procs";
    std::string failcntTestPath = "/proc/plimits/test/memory.failcnt";
    std::string usageTestPath = "/proc/plimits/test/memory.usage";
    std::string peakTestPath = "/proc/plimits/test/memory.peak";
    char buf[512];
    char writeBuf[8];
    int pid;
    char *stack = NULL;

    stack = (char *)mmap(NULL, twoM, PROT_READ | PROT_WRITE,
                         MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    ICUNIT_ASSERT_NOT_EQUAL(stack, NULL, 1);
    if (opendir(path.c_str()) == NULL) {
        ret = mkdir(path.c_str(), S_IFDIR | mode);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }
    if (opendir(path1.c_str()) == NULL) {
        ret = mkdir(path1.c_str(), S_IFDIR | mode);
        ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    }

    for (int i = 0; i < 10; ++i) {
        testStrArray2[i] = NULL;
        pid = clone(Clone, stack, CLONE_PARENT, &i);
        if (pid > 0) {
            std::string perfect = std::to_string(pid);
            ret = WriteFile(procsTestPath.c_str(), perfect.c_str());
            ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);
            sleep(1);
            ret = WriteFile(procsTestPath1.c_str(), perfect.c_str());
            ICUNIT_GOTO_NOT_EQUAL(ret, -1, ret, EXIT);
            waitpid(pid, &status, 0);
        }
    }
EXIT:
    for (int i = 0; i < 10; ++i) {
        free(testStrArray2[i]);
    }
    (void)RmdirControlFile(path);
    ret = rmdir(path.c_str());
    ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT1);
    return 0;

EXIT1:
    ret = rmdir(path.c_str());
    ICUNIT_ASSERT_EQUAL(ret, 0, ret);
    return 0;
}

void ItProcessPlimitsMemory014(void)
{
    TEST_ADD_CASE("IT_PROCESS_PLIMITS_MEMORY_014", Testcase, TEST_PROCESS, TEST_PLIMITS, TEST_LEVEL0, TEST_FUNCTION);
}
